import request from '@/utils/request'
import { getProvinceSelector } from '@/api/system/province'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        userId: undefined,
        provinceIds: [],
        description: undefined
      },
      userList: [],
      rules: {
        userId: [
          {
            required: true,
            message: '请输入招商经理',
            trigger: 'change'
          }
        ],
        provinceIds: [
          {
            required: true,
            message: '请输入负责省份',
            trigger: 'change'
          }
        ]
      },
      provinceOptions: []
    }
  },
  computed: {},
  watch: {},
  created() {
    // this.getProvinceList()
    this.getUserList()
    this.getAssignProvinceId()
  },
  mounted() {
  },
  methods: {
    init(id, isDetail) {
      this.dataForm.id = id || 0
      this.visible = true
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/campus/permission/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
            // this.$nextTick(() => {
            //   this.getAssignProvinceId()
            // })
          })
        }
        // else {
        //   this.getAssignProvinceId()
        // }
      })
    },
    getAssignProvinceId() {
      request({
        url: `/api/system/Area/-1/Selector/0`,
        method: 'get'
      }).then(res => {
        this.provinceOptions = res.data.list
      })
    },
    getUserList() {
      this.$store.dispatch('base/getUserList').then(res => {
        this.userList = res.filter(o => o.enabledMark == 1 && o.positionId.indexOf('445842201948194053') > -1)
      })
    },
    // getProvinceList() {
    //   getProvinceSelector(-1).then(res => {
    //     this.provinceOptions = res.data.list
    //   })
    // },
    dataFormSubmit() {
      this.searchTimer && clearTimeout(this.searchTimer);
      this.searchTimer = setTimeout(() => {
        console.log(123)
        this.$refs['elForm'].validate(valid => {
          if (valid) {
            this.loading = true
            if (!this.dataForm.id) {
              request({
                url: `/api/campus/permission`,
                method: 'post',
                data: this.dataForm
              }).then(res => {
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.loading = false;(this.visible = false), this.$emit('refresh', true)
                  }
                })
              })
            } else {
              request({
                url: '/api/campus/permission/' + this.dataForm.id,
                method: 'PUT',
                data: this.dataForm
              }).then(res => {
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.loading = false;
                    this.visible = false
                    this.$emit('refresh', true)
                  }
                })
              })
            }
          }
        })
      }, 300);

    }
  }
}
